<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
</head>

<body>
    <script>
        function myAxios(options) {
            let {
                url,
                method = 'get',
                data,
                params
            } = options

            if (method === 'get') {
                let arr = []
                for (const k in params) {
                    arr.push(`${k}=${params[k]}`)

                }
                url = url + `?${arr.join('&')}`
                console.log(url);
            }
            return new Promise((resolve, reject) => {
                // 1.实例化异步对象
                const xhr = new XMLHttpRequest()
                // 2.设置请求的 地址 和方法
                xhr.open(method, url)
                xhr.setRequestHeader('content-type', 'application/json')
                // 3.注册回调函数 服务器响应内容回来之后触发
                xhr.addEventListener('load', function () {
                    // console.log(xhr.response)
                    resolve(JSON.parse(xhr.response))
                })
                // 4.发送请求
                xhr.send(JSON.stringify(data))
            })
        }
        myAxios({
            url: 'http://ajax-api.itheima.net/api/robot',
            method: 'get',
            params: {
                spoken: '吃饭了吗?',
                name: '小黄人'
            }
        }).then((res) => {
            console.log(res)
        })
    </script>
</body>

</html>